import numpy as np

a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]  # 列表 List
a_ = np.array(a_list)  # 用列表创建numpy数组
b_ = np.arange(1, 11)  # 用 arange 函数创建numpy数组
dim = a_.ndim  # 1 维度 轴数
shape = a_.shape  # 形状  在每个轴上的大小， 形状是一个元组

# 创建二维行向量
row_vector = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
row_vector.ndim  # 2
row_vector.shape  # (1, 10) 一行十列
# 切片
row_vector[:, :2]  # :选取所有行， :2每行中索引0-1的元素 array([[1, 2]])
row_vector[:, 1:]  # 1: 从索引1一直到最后
row_vector[:, -3:]  # -3: 从倒数第三个到最后一个元素
# 创建二维列向量
col_vector = row_vector.T  # 行向量转置为列向量
# 切片
col_vector[:2, :]
col_vector[1:, :]
col_vector[-3:, :]


f1 = np.array([1, 2, 3, 4 ,5, 6])
for e in f1:
    if e % 2 == 0:
        print(e, 'ou')
    else:
        print(e, 'ji')

print('==================')

f2 = np.array([[-3, -2, -1, 0, 1, 2, 3]])
for i in f2:
    for j in i:
        if j > 0:
            print(j, '>0')
        if j < 2:
            print(j, '<2')
        if j <= 2:
            print(j, '<=2')

print('==================')

f3 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
f4 = f3.T  # 一维数组转置不变
print(f4)

print('==================')

f5 = np.array([[-3, -1, -1, 0, 1, 2, 3]])
print(f5.T)  # 行向量转为列向量

print('==================')

f6 = np.linspace(0, 10, num=5)
print(f6)
print('==================')
f7 = np.array([[[1, 1, 1],[1, 1, 1]],[[1, 1, 1],[1, 1, 1]]])
print(f7.shape)

print('==================')

import matplotlib.pyplot as plt

# plt.plot([1, 2, 3], color='red')
# plt.plot([1, 2, 3], c='skyblue')
# plt.show()

# x = [1, 2, 3, 4]
# y = [1, 4, 2, 4]
# # 创建图形和坐标轴
# plt.figure()
# # 绘图
# plt.plot(x, y)
# # 添加标签标题
# plt.xlabel('X轴')
# plt.ylabel('Y轴')
# plt.title("fk")
# plt.show()


# x = np.linspace(0, 10, 100)
# y = np.sin(x)
# plt.plot(x, y, color='red', linestyle='--', linewidth=2, label='sin(x)')
# plt.legend()  # 显示图例
# plt.show()

# x = np.random.randn(50)
# y = np.random.randn(50)
# colors = np.random.rand(50)
# sizes = 1000 * np.random.rand(50)
#
# plt.scatter(x, y, c=colors, s=sizes, alpha=0.6, cmap='viridis')
# plt.colorbar()  # 显示颜色条
# plt.show()


# categories = ['A', 'B', 'C', 'D']
# values = [3, 7, 2, 5]
# plt.bar(categories, values, color='skyblue')
# plt.show()

# sizes = [15, 30, 45, 10]
# labels = ['A', 'B', 'C', 'D']
# plt.pie(sizes, labels=labels, autopct='%1.1f%%')
# plt.show()

import matplotlib.pyplot as plt
import numpy as np

w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U**2 + V**2)

fig, axs = plt.subplots(3, 2, figsize=(7, 9), height_ratios=[1, 1, 2])
axs = axs.flat

#  Varying density along a streamline
axs[0].streamplot(X, Y, U, V, density=[0.5, 1])
axs[0].set_title('Varying Density')

# Varying color along a streamline
strm = axs[1].streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn')
fig.colorbar(strm.lines)
axs[1].set_title('Varying Color')

#  Varying line width along a streamline
lw = 5*speed / speed.max()
axs[2].streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)
axs[2].set_title('Varying Line Width')

# Controlling the starting points of the streamlines
seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1,  0, 1, 2, 2]])

strm = axs[3].streamplot(X, Y, U, V, color=U, linewidth=2,
                         cmap='autumn', start_points=seed_points.T)
fig.colorbar(strm.lines)
axs[3].set_title('Controlling Starting Points')

# Displaying the starting points with blue symbols.
axs[3].plot(seed_points[0], seed_points[1], 'bo')
axs[3].set(xlim=(-w, w), ylim=(-w, w))

# Create a mask
mask = np.zeros(U.shape, dtype=bool)
mask[40:60, 40:60] = True
U[:20, :20] = np.nan
U = np.ma.array(U, mask=mask)

axs[4].streamplot(X, Y, U, V, color='r')
axs[4].set_title('Streamplot with Masking')

axs[4].imshow(~mask, extent=(-w, w, -w, w), alpha=0.5, cmap='gray',
              aspect='auto')
axs[4].set_aspect('equal')

axs[5].streamplot(X, Y, U, V, broken_streamlines=False)
axs[5].set_title('Streamplot with unbroken streamlines')

plt.tight_layout()
plt.show()